<?php $title = "栏目"; ?>
<!DOCTYPE html>
<html>

<head>
    <?php include('css.php'); ?>
</head>

<body>

    <?php include('nav.php'); ?>
    <div class="main-content">
        <div class="container-fluid">
            <div class="row mb-4">
                <div class="col">
                    <h1 class="page-title"><?php echo $title; ?></h1>
                    <div class="mt-3">
                        <a href="catalog.php?menu=add_catalog" class="btn btn-primary">新建栏目</a>
                        <a href="catalog.php?menu=list_catalog" class="btn btn-primary">栏目列表</a>
                    </div>
                    <div class="mt-3 mb-3">
                        <?php
                        if (isset($_GET['menu'])) {
                            $menu = $_GET['menu'];
                            switch ($menu) {
                                case 'add_catalog':
                        ?>
                                    <form method="POST" action="toset.php?action=menu">
                                        <div class="mb-3">
                                            <label class="form-label" for="menu">栏目名称</label>
                                            <input class="form-control form-control-lg" id="menu" name="menu">
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">是否在首页显示</label>
                                            <input class="form-check-input" type="radio" name="echo" value="1" id="echo">
                                            是
                                            <input class="form-check-input" type="radio" name="echo" value="0" id="no">
                                            否
                                        </div>
                                        <div class="mb-3">
                                            <label class="form-label">是否在导航显示</label>
                                            <input class="form-check-input" type="radio" name="nav" value="1" id="nav">
                                            是
                                            <input class="form-check-input" type="radio" name="nav" value="0" id="no">
                                            否
                                        </div>
                                        <div class="mb-3">
                                            <input type="submit" class="btn btn-primary" value="添加栏目">
                                        </div>
                                    </form>
                                <?php break;
                                case 'list_catalog': ?>
                                    <div class="grid" style="--bs-columns: 10; --bs-gap: 1rem;">
                                        <div class="row align-items-center mt-3 mb-3">
                                            <div class="col text-center">
                                                <h5>ID</h5>
                                            </div>
                                            <div class="col text-center">
                                                <h5>栏目名称</h5>
                                            </div>
                                            <div class="col text-center">
                                                <h5>操作</h5>
                                            </div>
                                        </div>
                                        <?php $sql = "SELECT * FROM article_menu ORDER BY wbtreeid DESC";
                                        $result = mysqli_query($link, $sql);
                                        while ($row = mysqli_fetch_array($result)) {
                                            $wbtreeid = htmlspecialchars($row['wbtreeid']);
                                            $menu = htmlspecialchars($row['menu']); ?>
                                            <div class="row align-items-center mt-3 mb-3">
                                                <div class="col text-center"><?php echo $wbtreeid; ?></div>
                                                <div class="col text-center"><?php echo $menu; ?></div>
                                                <div class="col text-center">
                                                    <a href="JavaScript:;" onclick="if(confirm('包纸提示：您确定要删除 {<?php echo $menu; ?>} 这个栏目吗？')){ actionControl('delmenu', '<?php echo $wbtreeid; ?>'); }" class="btn btn-danger">删除</a>
                                                    <a href="catalog.php?menu=tree_catalog&wbtreeid=<?php echo $wbtreeid; ?>" class="btn btn-info">修改</a>
                                                </div>
                                            </div>
                                        <?php } ?>
                                        <script type="text/javascript">
                                            function actionControl(actionType, wbtreeid) {
                                                var xhr = new XMLHttpRequest();
                                                var requestUrl = 'toset.php?action=' + encodeURIComponent(actionType) + '&wbtreeid=' + encodeURIComponent(wbtreeid);
                                                xhr.open('GET', requestUrl, true);
                                                xhr.onreadystatechange = function() {
                                                    if (xhr.readyState === 4) {
                                                        if (xhr.status === 200) {
                                                            location.reload();
                                                        } else {
                                                            alert('出错！状态码：' + xhr.status);
                                                        }
                                                    }
                                                };
                                                xhr.send();
                                            }
                                        </script>

                                    </div>
                                <?php
                                    break;
                                case 'tree_catalog':
                                    $wbtreeid = $_GET['wbtreeid'];
                                    $sql = "select * from article_menu where wbtreeid = $wbtreeid";
                                    $result = $link->query($sql);
                                    $row = mysqli_fetch_array($result); ?>
                                    <?php if (empty($wbtreeid)) { ?>
                                        <p>栏目不存在！</p>
                                    <?php } else { ?>
                                        <form method="post" action="toset.php?action=updatemenu&wbtreeid=<?php echo $row['wbtreeid']; ?>">
                                            <div class="mb-3">
                                                <label class="form-label">修改栏目标题</label>
                                                <input class="form-control form-control-lg" value="<?php echo $row['menu']; ?>" name="menu">
                                            </div>
                                            <div class="mb-3">
                                                <label class="form-label">是否在首页显示</label>
                                                <input class="form-check-input" type="radio" name="echo" value="1" id="echo" <?php echo ($row['status'] == 1) ? 'checked' : ''; ?>>是
                                                <input class="form-check-input" type="radio" name="echo" value="0" id="no" <?php echo ($row['status'] == 0) ? 'checked' : ''; ?>>否
                                            </div>
                                            <div class="mb-3">
                                                <label class="form-label">是否在导航显示</label>
                                                <input class="form-check-input" type="radio" name="nav" value="1" id="nav" <?php echo ($row['nav'] == 1) ? 'checked' : ''; ?>>是
                                                <input class="form-check-input" type="radio" name="nav" value="0" id="no" <?php echo ($row['nav'] == 0) ? 'checked' : ''; ?>>否
                                            </div>
                                            <div class="mb-3">
                                                <input type="submit" class="btn btn-primary" value="修改栏目">
                                            </div>
                                        </form>
                                    <?php } ?>
                        <?php break;
                            }
                        } ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <?php include('bottom.php'); ?>
</body>

</html>